var FG;
if (!FG)
    FG = {};


FG.SelfUserView = DX.BBView.extend({
    tagField: 'username',
    el: '#user_pane',
    initialize: function() {
        var this_ = this;
        // this.username = this.$el.attr('username');
        this.username = FG.getUserStatus('username');
        this.user = new FG.User({
            id: this.username
        });
        this.cashAccount = new FG.Account({
            id: this.username + ':cash'
        });
        this.giftAccount = new FG.Account({
            id: this.username + ':gift'
        });
        this.update();
    },

    defaultValues: {
        username: "",
        respect_name: "",
        cash: "",
        gift: ""
    },

    template: _.template(
        '<div class="button_row bigtoolbutton_row">' +
            '<div class="button bigtoolbutton logout" style="float:right" title="登出"><i class="fa fa-sign-out"></i><span>登出</span></div>' +
            '<div class="button bigtoolbutton edit" id="user_edit" style="float:right" title="编辑"><i class="fa fa-edit"></i><span>编辑</span></div>' +
            '<div class="button bigtoolbutton refresh" style="float:right" title="刷新"><i class="fa fa-refresh"></i><span>刷新</span></div>' +
        '</div>' +
        '<table class="userinfo_table">' +
        '<tbody>' +
            '<tr><td class="tlabel">用户名</td><td><%= username %></td></tr>' +
            '<tr><td class="tlabel">姓名</td><td><%= full_name %></td></tr>' +
            '<tr><td class="tlabel">称呼</td><td><%= zh_title %></td></tr>' +
            '<tr><td class="tlabel">公司</td><td><%= company_name %></td></tr>' +
            '<tr><td class="tlabel">电子邮件</td><td><%= email %></td></tr>' +
            '<tr><td class="tlabel">电话</td><td><%= phone %></td></tr>' +
            '<tr><td class="tlabel">QQ</td><td><%= QQ %></td></tr>' +
            '<tr><td class="tlabel">其他通讯工具</td><td><%= IM %></td></tr>' +
            '<tr><td class="tlabel">备注</td><td><%= note %></td></tr>' +
            //'<tr><td class="tlabel">账户余额</td><td><%= cash %></td></tr>' +
            //'<tr><td class="tlabel">赠送余额</td><td><%= gift %></td></tr>' +
        '</tbody>' +
        '</table>'
    ),

    render: function() {
        var data = _.clone(this.user.attributes);
        _.extend(data, {
            cash: this.cashAccount.get('amount'),
            gift: this.giftAccount.get('amount')
        });
        this.$el.html(this.template(data));

        return this;
    },

    update: function() {
        var this_ = this;
        var userFetchRes = this.user.fetch(),
            cashFetchRes = this.cashAccount.fetch(),
            giftFetchRes = this.giftAccount.fetch();

        $.when.apply(this_, [userFetchRes, cashFetchRes, giftFetchRes]).done(function() {
            this_.render();
        });
        //alertify.log("update user info");
    },

    events: {
        'click .refresh': function(e) {
            this.update();
            e.stopPropagation();
        },
        'click .edit': function(e) {
            var this_ = this;
            layer.open({
                type: 2,
                title: false,
                shadeClose: true,
                area: ['850px', '520px'],
                content: '/user/user_edit/?head=false',
                end: function() {
                    this_.update()
                }
            });
            e.stopPropagation();
        },
        'click .logout': function(e) {
            location.href = "/user/logout/";
            e.stopPropagation();
        }
    }
});


FG.UserPanel = function(opts) {
    var this_ = this;

    this.moduleInfo = {
        name: "userPanel",
        zhName: "用户面板",
        desc: "用户面板"
    };

    if (_.has(opts, 'app')) {
        this.app = opts.app;
    }

    this.app.toolPanel.addTab('user', {
        "zhName": this.moduleInfo.zhName,
        "tabClass": "",
        "tabGroup": "info",
        "iconClass": "fa fa-user",
        "paneID": "user_pane_contents"
    });

    this.selfUserView = new FG.SelfUserView({el: '#user_pane_contents'});

    this.userSettings = {};
    this.userStatus = {};

    this.loadUserSettings();
    this.loadUserStatus();
    
    this.connectSignals();
};

FG.UserPanel.prototype.loadUserSettings = function() {
    var this_ = this;
    $.ajax({
        dataType: "json",
        url: '/setting/user/',
        success: function(data) {
            this_.userSettings = data;
            DX.shout('user:settings:loaded', data);
        }
    });
};

FG.UserPanel.prototype.loadUserStatus = function() {
    var this_ = this;
    $.ajax({
        dataType: "json",
        url: '/setting/user_status/',
        success: function(data) {
            this_.userStatus = data;
            DX.shout('user:status:loaded', data);
        }
    });
      
};

FG.UserPanel.prototype.connectSignals = function() {
    var this_ = this;

    DX.on('bought', function(uri) {
        this_.selfUserView.update();
    })
};